package com.gjs.freechat.web.controller;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;

import com.gjs.freechat.server.business.UserManageBusiness;
import com.gjs.freechat.server.component.ChatSessionManage;
import com.gjs.freechat.server.component.UserSessionPool;
import com.gjs.freechat.server.component.bean.Conversation;
import com.gjs.freechat.server.component.bean.UserInfo;
import com.gjs.freechat.web.utils.MyWebUtils;

@Controller
public class UserManageViewPreprocessor {
	
	public static Logger logger = Logger.getLogger(UserManageViewPreprocessor.class);
	
	@Autowired
	private ChatSessionManage chatSession;
	
	@Autowired
	private UserManageBusiness userManageBusiness; 
	
	@Autowired
	private UserSessionPool convosPool;
	
	@RequestMapping(value={"/login"})
	public String login(Model model, @CookieValue(value="loginWay", defaultValue="1") int loginWay) {
//		System.out.println(loginWay);
		model.addAttribute("loginWay", loginWay);
		return "login";
	}
	
	@RequestMapping("/")
	public String mainPageInit(Model model, WebRequest webRequest, HttpSession session) {
		UserInfo userInfo = (UserInfo) webRequest.getAttribute("userInfo", WebRequest.SCOPE_SESSION);
		//model.addAttribute("userInfo", userInfo);
		Map<String, Object> convosStatistic = chatSession.getAllConvosWithStatistic(userInfo.getId());
		//convosPool.userLoginConvosRegister(userInfo.getId(), (List<Conversation>) convos.get("allConvos"));
		MyWebUtils.setHttpSessionAttrs(session, convosStatistic);
		
		
		return "main_page";
	}
}
